<%@page import="java.io.*,java.util.*,org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="com.entity.NewsDetail" %>
<%@ page import="com.NewsService.NewsService" %>
<%@ page import="com.NewsServiceImpl.NewsServiceImpl" %>
<%@ page import="java.net.URLEncoder" %>
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
        boolean filr=   ServletFileUpload.isMultipartContent(request);
        String uploadFilePath ="http://localhost:8080/webJdbc/images/";
        System.out.println(uploadFilePath);
         //准备上传的文件地址
        String path="http://localhost:8080/webJdbc/images/";
        File file = new File(path);
        //如果目录不存在，就创建目录
        if(!file.exists()){
            file.mkdir();
        }
        //判断文件表单提交是否正确
        if(filr){
             //创建文件上传的核心工厂
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //文件上传对象
            ServletFileUpload servletFileUpload  = new ServletFileUpload(fileItemFactory);
            //获取表单的文件对象
            List<FileItem> list= servletFileUpload.parseRequest(request);
            //第一大步：上传数据
            Iterator<FileItem> item=  list.iterator();
            String fieldTitle=null;
            String picPath=null;
            while (item.hasNext()){
                FileItem fileUpload=  item.next();
              boolean isfile =  fileUpload.isFormField();
              if(isfile){
                String  fieldName=  fileUpload.getFieldName();
                  if(fieldName.equals("title")){
                      fieldTitle= fileUpload.getString("utf-8");
                  }
              }else {
                  //文件名称
                  String name= URLEncoder.encode(fileUpload.getName(),"utf-8") ;
                  File file1 = new File(name);
                  if(file1.getName() !=null && !(file1.getName().equals(""))){
                      //将对应的文件上传到指定的值
                      File file2 = new File(path,URLEncoder.encode(file1.getName(),"utf-8"));
                      //完成文件写入
                      fileUpload.write(file2);
                      picPath=file2.getName();
                  }
              }
            }
            //第二大步：保存数据库信息
            NewsDetail newsDetail = new NewsDetail();
            newsDetail.setTitle(fieldTitle);
            newsDetail.setPicPath(path+"\\"+picPath);

            NewsService newsService = new NewsServiceImpl();
            int cont= newsService.ServiceSinsertNews(newsDetail);
            if(cont>0){
               request.getRequestDispatcher("index.jsp").forward(request,response);
            }else {
                System.out.println("上传失败");
            }
        }



   /* String uploadFileName = "";
       //验证提交的方式
    boolean isMultipart =    ServletFileUpload.isMultipartContent(request);

     //制定文件的上传地址
    String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/");

    System.out.println(uploadFilePath);
     File saveDir = new File(uploadFilePath);
    //如果目录不存在，就创建目录
    if(!saveDir.exists()){
        saveDir.mkdir();
    }

    //验证文件是否存以正确的方式提交
    if(isMultipart){
        //创建上传文件的核心对象
        FileItemFactory fileItemFactory = new DiskFileItemFactory();
        //创建核心的对象Servlet
        ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
        try {

            //获取所有的表单数据
            List<FileItem>  items=    servletFileUpload.parseRequest(request);
            Iterator<FileItem> item=  items.iterator();
            while(item.hasNext()){
                FileItem ite = (FileItem)item.next();
                if(!ite.isFormField()){
                    String fileName =  ite.getName();
                    if(fileName != null && !fileName.equals("")){
                        File fullFile = new File(fileName);
                        File saveFile = new File(uploadFilePath,fullFile.getName());//将文件保存到指定的路径
                        ite.write(saveFile);
                        uploadFileName = fullFile.getName();
                    }
                }else {
                   String fieldName =  ite.getFieldName();// 获得该字段名称
                    if(fieldName.equals("title")){
                        System.out.println("====="+fieldName);;//获得该字段值
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }



    }
*/
%>
<%--<%=uploadFilePath%>--%>

</body>
</html>
